{extend name="shared/layout" /}
{block name="title"}修改图文{/block}
{block name="css"}
    <style>
        #btn-upfile{position:relative;overflow:hidden;cursor:pointer;}
        #file{position:absolute;right:0;top:0;width:100px;height:100px;opacity:0; ilter:alpha(opacity=0);cursor:pointer;}
    </style>
{/block}
{block name="javascript"}
    <script type="text/javascript">
         $(document).ready(function () {
            $("#ok").bind("click", Save);
            $("#cancel").bind("click", function () {
                parent.layer.close(parent.layer_edit_index);
            });

            var fileEls = document.getElementById('file');
            var upFileBtnEls = document.getElementById('btn-upfile');
            var fullBase64;
            var base64;
            fileEls.onchange = function(evt){
                if (!window.FileReader) return;
                var files = evt.target.files;
                for (var i = 0, f; f = files[i]; i++) {  
                    if (!f.type.match('image.*')) {  
                        continue;  
                    }

                    var reader = new FileReader();
                    reader.onload = (function(theFile) {
                        return function(e) {
                            document.getElementById('previewImage').src = e.target.result;
                            fullBase64 = e.target.result;
                            appendFile(fullBase64);
                        };
                    })(f);
                    reader.readAsDataURL(f);
                }
            };            
         })

         function appendFile(entry){
            var img = new Image();
            img.src = entry;
            
            img.onload = function () {
                var that = this;
                //设置图片宽高

                var w = 90;
                var h = 65;
        
                //生成canvas
                var canvas = document.createElement('canvas');
                var ctx = canvas.getContext('2d');
                $(canvas).attr({width : w, height : h});
                ctx.drawImage(that, 0, 0, w, h);
                base64 = canvas.toDataURL('image/jpeg', 0.9);   //1z 表示图片质量，越低越模糊。
                var up_img = document.getElementById('up_img');
                up_img.value = entry;
            }
        }

        function Save() {
            layui.use(['layer'], function(){
                var title = $.trim($("input[name=title]").val());
                if (title.length == 0) {                
                    layer.tips('标题不能为空！', 'input[name=title]', { tips: [2, '#78BA32'] });
                    return false;
                }
                var surl = $.trim($("input[name=url]").val());
                if (surl.length == 0) {
                    layer.tips('跳转地址不能为空！', 'input[name=url]', { tips: [2, '#78BA32'] });
                    return false;
                }
                var pic = $.trim($("input[name=pic]").val());
                var base64pic = $.trim($("#up_img").val());
                if (pic.length == 0 && base64pic.length == 0) {                
                    layer.tips('请上传图片！', 'input[name=pic]', { tips: [2, '#78BA32'] });
                    return false;
                }       
            
                var load_index = layer.load(0, { shade: false });
                $.ajax({
                    type: "post",
                    url: "{:url('imagetext/edit')}?t=" + new Date(),
                    data: $("form").serialize(),
                    dataType: "json",
                    success: function (result) {
                        layer.close(load_index);
                        if (result.code == 0) {
                            layer.alert('保存成功！', function () {
                                if (typeof (parent.initTable) == "function")
                                    parent.initTable();
                                parent.layer.close(parent.layer_edit_index)
                            });
                        } else {
                            layer.alert(result.msg);
                        }
                    }
                });
            });
        }
    </script>
{/block}
{block name="content"}
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-content">
                        <form method="post" id="fmNew" class="form-horizontal">
                            <input type="hidden" name="base64pic" id="up_img" />
                            <input type="hidden" name="id" value="{$data.id}" />
                            <div class="form-group">
                                <label class="col-sm-2 control-label">标题：</label>
                                <div class="col-sm-8">
                                    <input type="text" id="txttitle" name="title" class="form-control" value="{$data.title}" placeholder="标题">
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">跳转URL：</label>
                                <div class="col-sm-8">
                                    <input type="text" name="url" id="txturl" class="form-control" value="{$data.url}" placeholder="跳转URL">
                                </div>
                            </div>                       
                            <div class="hr-line-dashed"></div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">图片地址：</label>
                                <div class="col-sm-6">
                                    <input type="text" name="pic" id="txtpic" class="form-control" value="{$data.pic}" placeholder="图片地址(上传或者直接填写外链)">                                    
                                </div>
                                <div class="col-sm-1">
                                    <img id="previewImage" style="height:34px;">
                                </div>
                                <div class="col-sm-1">
                                    <span id="btn-upfile" class="btn btn-warning btn-sm" style="margin-bottom:0;padding:6px 8px;">
                                        <i class="glyphicon glyphicon-folder-open" style="padding-right:3px;"></i> 选择上传<input id="file" type="file" name="img">
                                    </span>
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">描述：</label>
                                <div class="col-sm-8">
                                    <input type="text" name="desc" id="txtdesc" class="form-control" value="{$data.desc}" placeholder="描述(单图文回复时才有用)">
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                            <div class="form-group">
                            <label class="col-sm-2 control-label">备注：</label>
                            <div class="col-sm-8">
                                <textarea class="form-control diff-textarea" style="height:150px;" name="remark" id="txtremark" placeholder="备注">{$data.remark}</textarea>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                            <div class="form-group">
                                <div class="col-sm-12 col-sm-offset-5">
                                    <button class="btn btn-primary" id="ok" type="button">提交</button>
                                    <button class="btn btn-white" id="cancel" type="button">取消</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>        
{/block}